<?php

function link_to($content, $parameters, $html_opts = array()) {
	$created_link_params = (create_link($parameters, $html_opts));
	$hrefvars = $created_link_params[0];
	$html_options = $created_link_params[1];
	if (urldecode($_SERVER['REQUEST_URI']) == $hrefvars) {
		//dont link, link to unless current part
		return $content;
	}
	else {
		return "<a $html_options href='$hrefvars' >{$content}</a>";
	}
}

function url_for($params = array()) {
	//root route must always be first..doesnt have to be..but logically should be
	$all_routes = Route::getRouteTable();
	if (isset($all_routes[''])) { $root_route = $all_routes['']; unset($all_routes['']); }

	//if (isset($root_route) && count($params) < 3 && count($params) > 0) {
		//was tring to wirte root...ahh for later
	//}

	foreach($all_routes as $sTemplate => $aRoute) {
		$aTemplate = Route::aCleanUrl($sTemplate);

		$caArray = array('controller', 'action');
		foreach ($caArray as $ca) {
			if (isset($params[$ca])) {
				if (isset($aRoute[$ca])) { if ($aRoute[$ca] != $params[$ca]) { continue 2; } }
				else { //got to check if its an any clause
					if (is_int($template_key = array_search(':'.$ca, $aTemplate))) { $aTemplate[$template_key] = $params[$ca]; }
					else { echo 'link_to function weve got a route problem ', $ca, ' not found'; }
				}
				unset($params[$ca]);
			}
		}

		foreach($aTemplate as $t_k => $template_val) {
			if ($template_val[0] == ':') {
				$template_val_real = substr($template_val, 1);
				if (isset($params[$template_val_real])) { $aTemplate[$t_k] = $params[$template_val_real]; unset($params[$template_val_real]); }
				//elseif (isset($aRoute[$template_val_real])) { $aTemplate[$t_k] = $aRoute[$template_val_real]; }
				else { unset($aTemplate[$t_k]); }
			}
		}
		if (!empty($params)) {
			$tailArray = array();
			foreach($params as $param_k => $param_val) { $tailArray[] = $param_k.'='.$param_val; }
			$tail = '?'.implode('&', $tailArray);
		}
		else { $tail = ''; }

		return PUBLIC_HTML.(implode('/', $aTemplate)).$tail;
	}


/*
	foreach(Route::get_routes() as $sTemplate => $aRoute) {
		$aTemplate = Route::aClean_url($sTemplate);
		foreach(array('controller','action') as $ca) {
			if (isset($params[$ca]) && (!($aRoute[$ca][0] == ':' || $aRoute[$ca] == $params[$ca]))) { continue 2; }
			if (is_int($keyVar = array_search(':'.$ca, $aTemplate)) && isset($params[$ca])) { $aTemplate[$keyVar] = $params[$ca]; }
			unset($params[$ca]);
		}
		//to get here it must have matched ca
		foreach ($aTemplate as $k => $v) {
			if ($aTemplate[$k][0] == ':') {
				$paramKey = substr($aTemplate[$k], 1);
				if (isset($params[$paramKey])) { $aTemplate[$k] = $params[$paramKey]; }
				else { unset($aTemplate[$k]); }
				unset($params[$paramKey]);
			}
		}
		$unmatchedTemplateItems = array();
		foreach($params as $k => $v) { $unmatchedTemplateItems[] = $k .'='. $v; }
		return PUBLIC_HTML.implode('/', $aTemplate) . ((count($unmatchedTemplateItems) > 0) ? '?'.implode('&', $unmatchedTemplateItems) : '');
		break;
	}

 */
}


function delete_link($content, $parameters, $html_opts = array()) {
	$created_link_params = (create_link($parameters, $html_opts));
	$hrefvars = $created_link_params[0];
	$html_options = $created_link_params[1];
	$onc = "if (confirm('Are you sure?')) { var f = document.createElement('form');
	f.style.display = 'none'; this.parentNode.appendChild(f); f.method = 'POST'; f.action = this.href;
	var m = document.createElement('input'); m.setAttribute('type', 'hidden'); m.setAttribute('name', '_method');
	m.setAttribute('value', 'delete'); f.appendChild(m);f.submit(); };return false;";
	return "<a $html_options href='$hrefvars' onclick=\"{$onc}\" >{$content}</a>";

}

function create_link($parameters, $html_opts) {
	if (is_array($parameters)) { $hrefvars = url_for($parameters); }
	else { $hrefvars = $parameters; }
	$html_options = (empty($html_opts)) ? NULL : generate_html_options($html_opts);
	return array($hrefvars, $html_options);
}



?>